<template>
  <el-form :model="formModel" class="login-form" ref="elFormCom" :rules="rules">
    <el-form-item :prop="accountNumberKey">
      <el-input v-model="accountNumber" :placeholder="accountPlace" class="login-form-input" />
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="formModel.password" placeholder="请输入密码" class="login-form-input" />
    </el-form-item>
    <el-form-item class="login-form-btns">
      <el-button type="primary" class="login-form-btn" @click="submit" :loading="loading"
        >登 录</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, computed, defineEmits, ref } from 'vue'
const emit = defineEmits(['submit'])
const elFormCom = ref()
const props = defineProps({
  method: {
    type: String,
    default: 'ap',
    validator: (value) => {
      return ['ap', 'code', 'password'].includes(value)
    },
  },
  loading: {
    type: Boolean,
    default: false,
  },
})

const formModel = reactive({
  userName: '',
  password: '',
  email: '',
  phonenumber: '',
  code: '',
})
const rules = {
  userName: [
    {
      message: '请输入用户名',
      trigger: 'blur',
      validator: (rule, value) => {
        return props.method != 'ap' || !!value
      },
    },
  ],
  password: [
    {
      message: '请输入密码',
      trigger: 'blur',
      validator: (rule, value) => {
        return props.method != 'ap' || !!value
      },
    },
  ],
}

const accountNumberKey = computed(
  () =>
    ({
      ap: 'userName',
      code: 'phonenumber',
      password: 'email',
    })[props.method],
)
const accountNumber = computed({
  set(val) {
    formModel[accountNumberKey.value] = val
  },
  get() {
    return formModel[accountNumberKey.value]
  },
})
const accountPlace = computed(() => {
  return {
    ap: '请输入用户名',
    code: '请输入手机号',
    password: '请输入口令',
  }[props.method]
})

const submit = () => {
  elFormCom.value.validate((valid) => {
    if (valid) {
      emit('submit', formModel)
    }
  })
}
</script>

<style lang="scss">
.login-form {
  &-btns {
    margin-bottom: 0;
  }

  &-btn {
    width: 100%;
    height: 58px;
    font-size: 20px;
  }

  &-input {
    height: 58px;
    font-style: 16px;
  }
}
</style>
